using System;
using System.Collections;
using System.Collections.Generic;
using BCW.Friends;
using ExitGames.Client.Photon;
using UnityEngine;

public class RaceController : MonoBehaviour
{
	public static RaceController instance;

	[SerializeField]
	private int maxPlayerCount;

	[SerializeField]
	private int minPlayerCount;

	[SerializeField]
	private Transform[] startPositions;

	[SerializeField]
	private List<RaceCheckPoint> checkPoints;

	[SerializeField]
	private RaceFinish finish;

	public List<RaceResult> raceResult = new List<RaceResult>();

	public bool enableMove;

	private int spawnIndex;

	private float _currentTimer;

	private bool gameIsStarted;

	public bool gameIsEnded;

	private bool timeStarted;

	private List<PlayerBehavior> playerResp;

	public List<RaceTrackPath> raceTrackPath;

	private int timeFindPlayers = 15;

	private int timeFinalCountdown = 5;

	public bool isFindPlayerState;

	public bool isFinalCountdownState;

	[SerializeField]
	private List<PlayerRace> playersRace;

	[SerializeField]
	private int circleCount = 10;

	public bool leaveRoom;

	private Coroutine _checkTimers;

	public int _playerCountInRace;

	private int activeCheckPointIndex;

	private float currentTimer
	{
		get
		{
			return _currentTimer;
		}
		set
		{
			_currentTimer = value;
		}
	}

	private void Awake()
	{
		instance = this;
		playerResp = new List<PlayerBehavior>(startPositions.Length);
		playersRace = new List<PlayerRace>(startPositions.Length);
		for (int i = 0; i < startPositions.Length; i++)
		{
			playerResp.Add(null);
			playersRace.Add(null);
		}
		ActivateCheckPoint(0);
	}

	private void Start()
	{
		MapWindow.instance.gameObject.SetActive(false);
	}

	public Transform GetRespanw()
	{
		return startPositions[spawnIndex];
	}

	public void SetSpawnIndex(int index)
	{
		spawnIndex = index;
	}

	public void SaveSpawnIndexPlayer(int index, PlayerBehavior player)
	{
		playerResp[index] = player;
		playersRace[index] = new PlayerRace(player);
	}

	private void OnDestroy()
	{
		if (instance == this)
		{
			instance = null;
		}
	}

	public void SpawnCar()
	{
		Transform respanw = GetRespanw();
		PrivateCarManager.instance.CarInstantiate(settings.RaceCarName);
		PrivateCarManager.instance.CallCar(respanw, false);
		Invoke("GetIn", 0.1f);
		PrivateCarManager.instance.currentCar.transform.position = respanw.position;
		PrivateCarManager.instance.currentCar.transform.rotation = respanw.rotation;
	}

	private void GetIn()
	{
		PlayerBehavior.MyPlayer.GetInCarWithConfirmation(PlayerBehavior.MyPlayer.viewId, PrivateCarManager.instance.currentCar.viewId);
	}

	private void Update()
	{
		if (enableMove && PrivateCarManager.instance.currentCar != null)
		{
			if (Time.frameCount % 7 == 0)
			{
				SetPlaceLabels();
			}
			EntityBehavior currentCar = PrivateCarManager.instance.currentCar;
			float num = ((!(currentCar.GetCurrentSpeed() + 0.5f > currentCar.GetMaxSpeed())) ? currentCar.GetCurrentSpeed() : currentCar.GetMaxSpeed());
			GameInfoWindow.instance.raceInfoUI.SetSpeed(((int)(num * 1.2f)).ToString());
			int num2 = (int)(PhotonNetwork.time - (double)currentTimer);
			GameInfoWindow.instance.raceInfoUI.SetTime(num2 / 60 + " : " + num2 % 60);
		}
	}

	private IEnumerator CheckTimers()
	{
		while (true)
		{
			yield return null;
			if (!PhotonNetwork.inRoom || PhotonNetwork.time == 0.0)
			{
				continue;
			}
			if (PhotonNetwork.room.customProperties[controllerConnectPhoton._FinalCountdown] != null)
			{
				currentTimer = (float)PhotonNetwork.room.customProperties[controllerConnectPhoton._FinalCountdown];
				if (!isFinalCountdownState)
				{
					if (!isFindPlayerState && !IsWaitPlayerForStart())
					{
						if (!leaveRoom)
						{
							controllerConnectPhoton.thisScript.LeaveRoom(false);
							leaveRoom = true;
						}
						continue;
					}
					isFinalCountdownState = true;
					StartFinalCountdown();
				}
			}
			else if (PhotonNetwork.room.customProperties[controllerConnectPhoton._StartRaceTime] != null)
			{
				currentTimer = (float)PhotonNetwork.room.customProperties[controllerConnectPhoton._StartRaceTime];
				if (!isFindPlayerState)
				{
					isFindPlayerState = true;
					gameIsStarted = true;
					StartGameWindow.instance.StartTimer((float)((double)currentTimer - PhotonNetwork.time), true);
				}
			}
			else
			{
				currentTimer = 0f;
			}
			if (!gameIsStarted && (double)currentTimer < PhotonNetwork.time && !isFinalCountdownState && !isFindPlayerState && IsEnoughPlayersInRace())
			{
				SetStartRaceGame((float)PhotonNetwork.time + (float)timeFindPlayers);
				gameIsStarted = true;
			}
			if (!isFinalCountdownState && currentTimer != 0f && isFindPlayerState && ((PhotonNetwork.time >= (double)currentTimer && IsEnoughPlayersInRace()) || IsRoomFull()))
			{
				SetFinalCountdown((float)PhotonNetwork.time + (float)timeFinalCountdown);
				StartFinalCountdown();
			}
			if (currentTimer != 0f && !enableMove && PhotonNetwork.time >= (double)currentTimer && isFinalCountdownState)
			{
				StartRace();
			}
			if (PhotonNetwork.room.visible != !isFinalCountdownState)
			{
				PhotonNetwork.room.visible = !isFinalCountdownState;
			}
		}
	}

	private bool IsEnoughPlayersInRace()
	{
		int colReady = 0;
		GameController.instance.listPlayers.ForEach(delegate(PlayerBehavior p)
		{
			if (p.playerReadyRace)
			{
				colReady++;
			}
		});
		return colReady >= minPlayerCount;
	}

	private bool IsWaitPlayerForStart()
	{
		return GameController.instance.listPlayers.Count > 1 && GameController.instance.listPlayers.Count - 1 < minPlayerCount;
	}

	private void StartRace()
	{
		if (PrivateCarManager.instance.currentCar != null)
		{
			Transform respanw = GetRespanw();
			PrivateCarManager.instance.currentCar.transform.position = respanw.position;
			PrivateCarManager.instance.currentCar.transform.rotation = respanw.rotation;
		}
		if (GameInfoWindow.instance.resultWindow != null)
		{
			GameInfoWindow.instance.resultWindow.Init();
		}
		for (int i = 0; i < playersRace.Count; i++)
		{
			if (playersRace[i] != null)
			{
				if (playersRace[i].player != null)
				{
					playersRace[i].circleIndex = 0;
					playersRace[i].prevLineIndex = 0;
					playersRace[i].checkPointIndex = 0;
					playersRace[i].timeEnded = 0f;
				}
				else
				{
					playersRace[i] = null;
				}
			}
		}
		enableMove = true;
		PlayerBehavior.MyPlayer.playerReadyRace = false;
		this.raceResult.Clear();
		foreach (PlayerBehavior listPlayer in GameController.instance.listPlayers)
		{
			RaceResult raceResult = new RaceResult();
			raceResult.name = listPlayer.nick;
			raceResult.idPlayer = listPlayer.props.playerId;
			raceResult.time = 0f;
			if (raceResult.idPlayer != null && raceResult.name != null)
			{
				this.raceResult.Add(raceResult);
			}
		}
		GameInfoWindow.instance.ShowRaceInfo(true);
		GameInfoWindow.instance.raceInfoUI.SetCircle("1/" + circleCount);
		_playerCountInRace = GetPlayerCount();
		Dictionary<string, string> dictionary = new Dictionary<string, string>();
		string text = ((!string.IsNullOrEmpty(PhotonNetwork.room.customProperties["pass"].ToString())) ? "private" : string.Empty);
		dictionary.Add("Tier " + CupsManager.CurrentTier, "Race " + text);
		FlurryWrapper.LogEvent(FlurryWrapper.EV_MULTIPLAYER_MODE_RATING, dictionary);
		dictionary.Clear();
		dictionary.Add("Round", "Race");
		FlurryWrapper.LogEvent(FlurryWrapper.EV_ROUNDS_COUNT_BY_MODES, dictionary);
	}

	private bool IsRoomFull()
	{
		int colReady = 0;
		GameController.instance.listPlayers.ForEach(delegate(PlayerBehavior p)
		{
			if (p.playerReadyRace)
			{
				colReady++;
			}
		});
		return colReady == PhotonNetwork.room.maxPlayers;
	}

	private void StartFinalCountdown()
	{
		GameInfoWindow.instance.Hide();
		MapWindow.instance.gameObject.SetActive(true);
		StartGameWindow.instance.StartTimer((float)((double)currentTimer - PhotonNetwork.time), false);
		GC.Collect();
		Resources.UnloadUnusedAssets();
		if (PrivateCarManager.instance.currentCar != null)
		{
			Transform respanw = GetRespanw();
			PrivateCarManager.instance.currentCar.transform.position = respanw.position;
			PrivateCarManager.instance.currentCar.transform.rotation = respanw.rotation;
		}
	}

	private void SetStartRaceGame(float time)
	{
		isFindPlayerState = true;
		currentTimer = time;
		PhotonNetwork.room.SetCustomProperties(new ExitGames.Client.Photon.Hashtable { 
		{
			controllerConnectPhoton._StartRaceTime,
			time
		} });
		StartGameWindow.instance.StartTimer((float)((double)currentTimer - PhotonNetwork.time), true);
	}

	private void SetFinalCountdown(float time)
	{
		isFinalCountdownState = true;
		currentTimer = time;
		PhotonNetwork.room.SetCustomProperties(new ExitGames.Client.Photon.Hashtable { 
		{
			controllerConnectPhoton._FinalCountdown,
			time
		} });
	}

	private int PlayerCirclePassed(PlayerBehavior player)
	{
		PlayerRace playerRace = playersRace.Find((PlayerRace p) => p != null && p.player == player);
		if (playerRace != null && player != null)
		{
			playerRace.circleIndex++;
			playerRace.checkPointIndex = 0;
			return playerRace.circleIndex;
		}
		return 0;
	}

	private int GetPlayerCircleNumber(PlayerBehavior player)
	{
		PlayerRace playerRace = playersRace.Find((PlayerRace p) => p != null && p.player == player);
		if (playerRace != null && player != null)
		{
			return playerRace.circleIndex;
		}
		return 0;
	}

	private void SetPlayerCircleNumber(PlayerBehavior player, int number)
	{
		PlayerRace playerRace = playersRace.Find((PlayerRace p) => p != null && p.player == player);
		if (playerRace != null && player != null)
		{
			playerRace.circleIndex = number;
			playerRace.checkPointIndex = 0;
		}
	}

	private int GetPlayerCheckPointNumber(PlayerBehavior player)
	{
		PlayerRace playerRace = playersRace.Find((PlayerRace p) => p != null && p.player == player);
		if (playerRace != null && player != null)
		{
			return playerRace.checkPointIndex;
		}
		return 0;
	}

	private void SetPlayerCheckPointNumber(PlayerBehavior player, int number)
	{
		PlayerRace playerRace = playersRace.Find((PlayerRace p) => p != null && p.player == player);
		if (playerRace != null && player != null)
		{
			playerRace.checkPointIndex = number;
		}
	}

	private void SetPlayerLineIndex(PlayerBehavior player, int number)
	{
		PlayerRace playerRace = playersRace.Find((PlayerRace p) => p != null && p.player == player);
		if (playerRace != null && player != null)
		{
			playerRace.prevLineIndex = number;
		}
	}

	public void AddEndRaceResult(PlayerBehavior player, string name, float time)
	{
		time = (float)(int)(time * 100f) / 100f;
		RaceResult item = raceResult.Find((RaceResult x) => x.name == player.nick && x.time.Equals(0f));
		raceResult.Remove(item);
		item = new RaceResult();
		item.name = name;
		item.time = time;
		item.mine = player == PlayerBehavior.MyPlayer;
		item.idPlayer = player.props.playerId;
		if (item.idPlayer != null && item.name != null)
		{
			raceResult.Add(item);
		}
		raceResult.Sort(delegate(RaceResult r1, RaceResult r2)
		{
			if (r1.time.Equals(r2.time))
			{
				return 0;
			}
			if (r1.time.Equals(0f))
			{
				return 1;
			}
			return r2.time.Equals(0f) ? (-1) : ((r1.time > r2.time) ? 1 : (-1));
		});
		if (gameIsEnded)
		{
			if (player == PlayerBehavior.MyPlayer)
			{
				Invoke("ShowEndGameWindow", 0.6f);
			}
			else
			{
				GameInfoWindow.instance.UpdateScrollForCurrentGameMode();
			}
		}
		PlayerRace playerRace = playersRace.Find((PlayerRace p) => p != null && p.player == player);
		if (playerRace != null && player != null)
		{
			playerRace.timeEnded = time;
		}
		if (player.currentCar != null)
		{
			StartCoroutine(StartBlinkingCar(player.currentCar as CarBehavior));
		}
	}

	private IEnumerator StartBlinkingCar(CarBehavior car)
	{
		yield return new WaitForSeconds(0.5f);
		car.StartBlinking();
		car._lerpTransformPhoton.enabled = false;
	}

	private void ShowEndGameWindow()
	{
		GameInfoWindow.instance.ShowEndGameWindow();
	}

	public void Restart()
	{
		if (_checkTimers != null)
		{
			StopCoroutine(_checkTimers);
			_checkTimers = null;
		}
		if (FriendsUIGame.instance != null)
		{
			FriendsUIGame.instance.CleanInvitePlayer();
		}
		MapWindow.instance.gameObject.SetActive(false);
		enableMove = false;
		PlayerBehavior.MyPlayer.playerReadyRace = true;
		bool flag = gameIsEnded;
		if (gameIsEnded)
		{
			controllerConnectPhoton.thisScript.LeaveRoom();
		}
		timeStarted = false;
		gameIsEnded = false;
		gameIsStarted = false;
		currentTimer = 0f;
		raceResult.Clear();
		finish.gameObject.SetActive(false);
		ActivateCheckPoint(0);
		_playerCountInRace = 0;
		GameInfoWindow.instance.raceInfoUI.SetCircle(0 + "/" + circleCount);
		GC.Collect();
		Resources.UnloadUnusedAssets();
		if (!flag)
		{
			_checkTimers = StartCoroutine(CheckTimers());
		}
	}

	public void ResetFlags()
	{
		isFinalCountdownState = false;
		isFindPlayerState = false;
		gameIsStarted = false;
		leaveRoom = false;
		GameInfoWindow.instance.ShowRaceInfo(false);
	}

	public void GetSpawnIndexForPlayer(PlayerBehavior player)
	{
		if (!playerResp.Contains(player))
		{
			for (int i = 0; i < playerResp.Count; i++)
			{
				if (playerResp[i] == null)
				{
					playerResp[i] = player;
					playersRace[i] = new PlayerRace(player);
					break;
				}
			}
		}
		for (int j = 0; j < playerResp.Count; j++)
		{
			if (playerResp[j] != null)
			{
				playerResp[j].SendRespawnIndex(j);
			}
		}
	}

	public string GetRacePlace()
	{
		return GetPlacePlayer(null) + "/" + _playerCountInRace;
	}

	public int GetPlayerCount()
	{
		int playerCount = 0;
		playersRace.ForEach(delegate(PlayerRace p)
		{
			if (p != null && (p.player != null || p.timeEnded != 0f))
			{
				playerCount++;
			}
		});
		return playerCount;
	}

	public int GetPlacePlayer(PlayerBehavior player)
	{
		if (player == null)
		{
			player = PlayerBehavior.MyPlayer;
		}
		float playerDistance = GetPlayerDistance(player);
		float timeEnded = playersRace.Find((PlayerRace p) => p != null && p.player == player).timeEnded;
		int num = 1;
		for (int i = 0; i < playersRace.Count; i++)
		{
			if (playersRace[i] == null || !(playersRace[i].player != player))
			{
				continue;
			}
			if (timeEnded != 0f)
			{
				if (playersRace[i].timeEnded != 0f && playersRace[i].timeEnded < timeEnded)
				{
					num++;
				}
			}
			else if (playersRace[i].timeEnded != 0f)
			{
				num++;
			}
			else if (playersRace[i].player != null && GetPlayerDistance(playersRace[i].player) < playerDistance)
			{
				num++;
			}
		}
		return num;
	}

	public void SetPlaceLabels()
	{
		for (int i = 0; i < playersRace.Count; i++)
		{
			if (playersRace[i] == null)
			{
				continue;
			}
			if (playersRace[i].player != null && playersRace[i].player != PlayerBehavior.MyPlayer)
			{
				if (playersRace[i].player.currentCar != null)
				{
					playersRace[i].player.currentCar.racePlaceLabel.SetPlace(GetPlacePlayer(playersRace[i].player));
				}
			}
			else
			{
				GameInfoWindow.instance.raceInfoUI.SetPlace(GetRacePlace());
			}
		}
	}

	private float GetPlayerDistance(PlayerBehavior player)
	{
		Vector2 vector = new Vector2(player.transform.position.x, player.transform.position.z);
		int nearestIndex = GetNearestIndex(vector, GetPlayerCheckPointNumber(player));
		nearestIndex = GetNearestLineIndex(nearestIndex, vector);
		if (LineIsPassed(nearestIndex, vector))
		{
			nearestIndex++;
		}
		if (nearestIndex >= raceTrackPath.Count)
		{
			nearestIndex = 0;
		}
		SetPlayerLineIndex(player, nearestIndex);
		return (float)(100000 * (circleCount - GetPlayerCircleNumber(player)) + (raceTrackPath.Count - nearestIndex) * 1000) + DistanceTo(raceTrackPath[nearestIndex].p0, raceTrackPath[nearestIndex].p1, vector);
	}

	private int GetNearestIndex(Vector2 position, int checkPoint)
	{
		float num = float.MaxValue;
		int result = 0;
		for (int i = 0; i < raceTrackPath.Count; i++)
		{
			if (checkPoint == raceTrackPath[i].checkPointIndex || raceTrackPath[i].checkPointIndex + 1 == checkPoint)
			{
				float num2 = Vector2.Distance(raceTrackPath[i].center, position);
				if (num2 < num)
				{
					num = num2;
					result = i;
				}
			}
		}
		return result;
	}

	private int GetNearestLineIndex(int index, Vector2 position)
	{
		float num = DistanceTo(raceTrackPath[index].p0, raceTrackPath[index].p1, position);
		int result = index;
		for (int i = index + 1; i < raceTrackPath.Count; i++)
		{
			float num2 = DistanceTo(raceTrackPath[i].p0, raceTrackPath[i].p1, position);
			if (num2 < num)
			{
				num = num2;
				result = i;
				continue;
			}
			break;
		}
		int num3 = index - 1;
		while (num3 > -1)
		{
			float num4 = DistanceTo(raceTrackPath[num3].p0, raceTrackPath[num3].p1, position);
			if (num4 < num)
			{
				num = num4;
				result = num3;
				num3--;
				continue;
			}
			break;
		}
		return result;
	}

	public float DistanceTo(Vector2 p0, Vector2 p1, Vector2 p)
	{
		Vector2 vector = p1 - p0;
		Vector2 lhs = p - p0;
		float num = 0f;
		float num2 = Vector2.Dot(lhs, vector);
		if (num2 <= 0f)
		{
			return Vector2.Distance(p, p0);
		}
		float num3 = Vector2.Dot(vector, vector);
		if (num3 <= num2)
		{
			return Vector2.Distance(p, p1);
		}
		float num4 = num2 / num3;
		Vector2 b = p0 + num4 * vector;
		return Vector2.Distance(p, b);
	}

	public bool LineIsPassed(int index, Vector2 position)
	{
		Vector2 p = raceTrackPath[index].p0;
		Vector2 p2 = raceTrackPath[index].p1;
		Vector2 vector = p2 - p;
		Vector2 lhs = position - p;
		float num = Vector2.Dot(lhs, vector);
		float num2 = Vector2.Dot(vector, vector);
		float num3 = num / num2;
		Vector2 lhs2 = p + num3 * vector - position;
		Vector2 zero = Vector2.zero;
		zero = ((index != 0) ? raceTrackPath[index - 1].center : raceTrackPath[raceTrackPath.Count - 1].center);
		lhs = zero - p;
		num = Vector2.Dot(lhs, vector);
		num2 = Vector2.Dot(vector, vector);
		num3 = num / num2;
		Vector2 rhs = p + num3 * vector - zero;
		return Vector2.Dot(lhs2, rhs) < 0f;
	}

	public void Respawn()
	{
		if (enableMove)
		{
			PlayerBehavior myPlayer = PlayerBehavior.MyPlayer;
			Vector2 vector = new Vector2(myPlayer.transform.position.x, myPlayer.transform.position.z);
			int nearestIndex = GetNearestIndex(vector, GetPlayerCheckPointNumber(myPlayer));
			nearestIndex = GetNearestLineIndex(nearestIndex, vector);
			if (LineIsPassed(nearestIndex, vector))
			{
				nearestIndex++;
			}
			Vector2 zero = Vector2.zero;
			zero = ((nearestIndex != 0) ? raceTrackPath[nearestIndex - 1].center : raceTrackPath[raceTrackPath.Count - 1].center);
			Vector2 vector2 = zero - raceTrackPath[nearestIndex].center;
			Vector2 lhs = vector - raceTrackPath[nearestIndex].center;
			float num = Vector2.Dot(lhs, vector2);
			float num2 = Vector2.Dot(vector2, vector2);
			float f = num / num2;
			Vector2 vector3 = raceTrackPath[nearestIndex].center + Mathf.Abs(f) * vector2;
			Vector3 origin = new Vector3(vector3.x, 20f, vector3.y);
			Ray ray = new Ray(origin, Vector3.down);
			RaycastHit hitInfo;
			if (Physics.Raycast(ray, out hitInfo, 100f, 1) && PrivateCarManager.instance.currentCar != null)
			{
				CarBehavior carBehavior = PrivateCarManager.instance.currentCar as CarBehavior;
				carBehavior.transform.position = hitInfo.point + Vector3.up * 0.4f;
				carBehavior.StartBlinking();
				carBehavior.transform.LookAt(new Vector3(raceTrackPath[nearestIndex].center.x, hitInfo.point.y, raceTrackPath[nearestIndex].center.y));
				carBehavior.transform.rotation = Quaternion.Euler(0f, carBehavior.transform.rotation.eulerAngles.y, 0f);
				carBehavior.rigidCar.velocity = Vector3.zero;
				Invoke("StopBlinking", 0.5f);
			}
		}
	}

	private void StopBlinking()
	{
		(PrivateCarManager.instance.currentCar as CarBehavior).StopBlinking();
	}

	public void CirclePassed()
	{
		ActivateCheckPoint(0);
		int num = PlayerCirclePassed(PlayerBehavior.MyPlayer);
		PlayerBehavior.MyPlayer.SendCircle(num, (float)PhotonNetwork.time - currentTimer);
		if (num == circleCount)
		{
			gameIsEnded = true;
			GameInfoWindow.instance.raceInfoUI.ClearCheckpointInfo();
			GameInfoWindow.instance.ShowRaceInfo(false);
			PlayerBehavior.MyPlayer.NotifyRaceEnded((float)PhotonNetwork.time - currentTimer);
			settings.IncreaseRoundCountForGameMode(TypeGame.race, 1);
			StarterPacksManager.instance.RoundCountEvents(TypeGame.race, settings.GetRoundCountForGameMode(TypeGame.race));
		}
		else
		{
			GameInfoWindow.instance.raceInfoUI.SetCircle(num + 1 + "/" + circleCount);
			finish.AddTimeForPlayer(PlayerBehavior.MyPlayer, (float)PhotonNetwork.time - currentTimer);
			StartCoroutine(GameInfoWindow.instance.raceInfoUI.ShowPassedCheckpoint(finish, num));
		}
	}

	public void CheckPointPassed(RaceCheckPoint point)
	{
		if (checkPoints[activeCheckPointIndex] != point)
		{
			BCWDebug.LogError("Нарушен порядок чекпоинтов");
		}
		ActivateCheckPoint(activeCheckPointIndex + 1);
		PlayerBehavior.MyPlayer.SendCheckPoint(activeCheckPointIndex, (float)PhotonNetwork.time - currentTimer);
		point.AddTimeForPlayer(PlayerBehavior.MyPlayer, (float)PhotonNetwork.time - currentTimer);
		StartCoroutine(GameInfoWindow.instance.raceInfoUI.ShowPassedCheckpoint(point, playersRace.Find((PlayerRace x) => x != null && x.player == PlayerBehavior.MyPlayer).circleIndex));
		SetPlayerCheckPointNumber(PlayerBehavior.MyPlayer, activeCheckPointIndex);
	}

	private void ActivateCheckPoint(int index)
	{
		checkPoints.ForEach(delegate(RaceCheckPoint c)
		{
			c.gameObject.SetActive(false);
		});
		if (index >= checkPoints.Count)
		{
			finish.gameObject.SetActive(true);
		}
		else
		{
			checkPoints[index].gameObject.SetActive(true);
			finish.gameObject.SetActive(false);
		}
		activeCheckPointIndex = index;
	}

	public void PlayerSetCheckPoint(PlayerBehavior player, int index, float passingTime)
	{
		SetPlayerCheckPointNumber(player, index);
		checkPoints[index - 1].AddTimeForPlayer(player, passingTime);
	}

	public void PlayerSetCircle(PlayerBehavior player, int index, float passingTime)
	{
		SetPlayerCircleNumber(player, index);
		finish.AddTimeForPlayer(player, passingTime);
	}
}
